﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        FillDataUnTypeOne();
    }

    
    void FillDataUnTypeOne() {
        DataTable table = ProductDAO.GetAllProducts();
        gvwData.DataSource = table;        
        gvwData.DataBind();
    }

    /*
     * Option 1 : Show by Linq to Dataset : Chỉ dùng để show sữ liệu, vì ở đây là chúng ta thao tác với "nơi chứa" Dataset chứ ko phải
     * là dữ liệu thật trong Database.
     * Do đặc thù của Linq, cho nênsuy đoán là linq sẽ chỉ có thể làm việc với Dataset Type (Disconnected Type).
     * 
     * Thay vi Add new item: Linq to SQL classes thi o day a dung luon table Product trong Dataset nhu mot doi tuong da 
     * duoc cau truc san nhu mot Class linq voi day du du lieu duoc chua tai dataset
     */
    void FillDataUntypeByLinq() {
        NorthwindDatasetTableAdapters.ProductsTableAdapter adapter = new NorthwindDatasetTableAdapters.ProductsTableAdapter();
        NorthwindDataset.ProductsDataTable table = new NorthwindDataset.ProductsDataTable();
        adapter.Fill(table);
        var query = from p in table
                    select new
                    {
                        ID = p.ProductID,
                        Name = p.ProductName,
                        Supplier = p.SupplierID,
                        Catelory = p.CategoryID
                    };
        gvwData.DataSource = query;
        gvwData.DataBind();
    }

    protected void btnShow_Click(object sender, EventArgs e)
    {
        FillDataUntypeByLinq();
    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        FillDataUnTypeOne();
    }

    void SearchByProductName(string name)
    {
        NorthwindDatasetTableAdapters.ProductsTableAdapter adapter = new NorthwindDatasetTableAdapters.ProductsTableAdapter();
        NorthwindDataset.ProductsDataTable table = new NorthwindDataset.ProductsDataTable();
        adapter.Fill(table);
        var query = from p in table
                    where p.ProductName.StartsWith(name)
                    select p;
        gvwData.DataSource = query;
        gvwData.DataBind();
    }

    void SearhBySupplierID(int id) {
        NorthwindDatasetTableAdapters.ProductsTableAdapter adapter = new NorthwindDatasetTableAdapters.ProductsTableAdapter();
        NorthwindDataset.ProductsDataTable table = new NorthwindDataset.ProductsDataTable();
        adapter.Fill(table);
        var query = from p in table
                    where Convert.ToInt32(p.SupplierID) == id
                    select p;
        gvwData.DataSource = query;
        gvwData.DataBind();
    }
    void SearchByCateloryID(int id) {
        NorthwindDatasetTableAdapters.ProductsTableAdapter adapter = new NorthwindDatasetTableAdapters.ProductsTableAdapter();
        NorthwindDataset.ProductsDataTable table = new NorthwindDataset.ProductsDataTable();
        adapter.Fill(table);
        var query = from p in table
                    where p.CategoryID == id
                    select p;
        gvwData.DataSource = query;
        gvwData.DataBind();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        int number = ddlSearch.SelectedIndex;
        switch (number)
        {
            case 0:
                break;
            case 1:
                string name = txtSearch.Text;
                SearchByProductName(name);
                break;
            case 2:
                int id = Convert.ToInt32(txtSearch.Text);
                SearhBySupplierID(id);
                break;
            case 3:
                int cid = Convert.ToInt32(txtSearch.Text);
                SearchByCateloryID(cid);
                break;
        }
    }
}